Plot Types¶
This notebook demonstrates all the plot types available in xarray_plotly.
In [1]:
Copied!
import numpy as np
import pandas as pd
import xarray as xr
from xarray_plotly import config, xpx
config.notebook() # Configure Plotly for notebook rendering
import numpy as np
import pandas as pd
import xarray as xr
from xarray_plotly import config, xpx
config.notebook() # Configure Plotly for notebook rendering
Sample Data¶
Let's create some sample data to work with:
In [2]:
Copied!
np.random.seed(42)
# Time series data
da_ts = xr.DataArray(
np.random.randn(30, 3).cumsum(axis=0),
dims=["time", "category"],
coords={
"time": pd.date_range("2024-01-01", periods=30),
"category": ["A", "B", "C"],
},
name="value",
)
# 2D grid data
da_2d = xr.DataArray(
np.random.rand(20, 30),
dims=["lat", "lon"],
coords={
"lat": np.linspace(-90, 90, 20),
"lon": np.linspace(-180, 180, 30),
},
name="temperature",
)
# Categorical data
da_cat = xr.DataArray(
np.random.rand(4, 3) * 100,
dims=["product", "region"],
coords={
"product": ["Widget", "Gadget", "Gizmo", "Thingamajig"],
"region": ["North", "South", "West"],
},
name="sales",
)
np.random.seed(42)
# Time series data
da_ts = xr.DataArray(
np.random.randn(30, 3).cumsum(axis=0),
dims=["time", "category"],
coords={
"time": pd.date_range("2024-01-01", periods=30),
"category": ["A", "B", "C"],
},
name="value",
)
# 2D grid data
da_2d = xr.DataArray(
np.random.rand(20, 30),
dims=["lat", "lon"],
coords={
"lat": np.linspace(-90, 90, 20),
"lon": np.linspace(-180, 180, 30),
},
name="temperature",
)
# Categorical data
da_cat = xr.DataArray(
np.random.rand(4, 3) * 100,
dims=["product", "region"],
coords={
"product": ["Widget", "Gadget", "Gizmo", "Thingamajig"],
"region": ["North", "South", "West"],
},
name="sales",
)
Line Plot¶
Best for time series and continuous data:
In [3]:
Copied!
fig = xpx(da_ts).line(title="Line Plot")
fig
fig = xpx(da_ts).line(title="Line Plot")
fig
With markers¶
In [4]:
Copied!
fig = xpx(da_ts).line(markers=True, title="Line Plot with Markers")
fig
fig = xpx(da_ts).line(markers=True, title="Line Plot with Markers")
fig
Bar Chart¶
Best for comparing categorical data:
In [5]:
Copied!
fig = xpx(da_cat).bar(title="Stacked Bar Chart")
fig
fig = xpx(da_cat).bar(title="Stacked Bar Chart")
fig
Grouped bars¶
In [6]:
Copied!
fig = xpx(da_cat).bar(barmode="group", title="Grouped Bar Chart")
fig
fig = xpx(da_cat).bar(barmode="group", title="Grouped Bar Chart")
fig
Area Chart¶
Best for showing composition over time:
In [7]:
Copied!
# Use absolute values for stacking to make sense
da_positive = xr.DataArray(
np.abs(np.random.randn(30, 3)) * 10,
dims=["time", "source"],
coords={
"time": pd.date_range("2024-01-01", periods=30),
"source": ["Solar", "Wind", "Hydro"],
},
name="energy",
)
fig = xpx(da_positive).area(title="Stacked Area Chart")
fig
# Use absolute values for stacking to make sense
da_positive = xr.DataArray(
np.abs(np.random.randn(30, 3)) * 10,
dims=["time", "source"],
coords={
"time": pd.date_range("2024-01-01", periods=30),
"source": ["Solar", "Wind", "Hydro"],
},
name="energy",
)
fig = xpx(da_positive).area(title="Stacked Area Chart")
fig
Scatter Plot¶
Best for showing relationships between variables:
In [8]:
Copied!
fig = xpx(da_ts).scatter(title="Scatter Plot")
fig
fig = xpx(da_ts).scatter(title="Scatter Plot")
fig
Dimension vs Dimension (Geographic style)¶
You can plot one dimension against another, with values shown as color:
In [9]:
Copied!
fig = xpx(da_2d).scatter(x="lon", y="lat", color="value", title="Lat/Lon Scatter")
fig
fig = xpx(da_2d).scatter(x="lon", y="lat", color="value", title="Lat/Lon Scatter")
fig
Box Plot¶
Best for showing distributions:
In [10]:
Copied!
# Create data with more samples
da_dist = xr.DataArray(
np.random.randn(100, 4) + np.array([0, 1, 2, 3]),
dims=["sample", "group"],
coords={"group": ["Control", "Treatment A", "Treatment B", "Treatment C"]},
name="response",
)
fig = xpx(da_dist).box(title="Box Plot")
fig
# Create data with more samples
da_dist = xr.DataArray(
np.random.randn(100, 4) + np.array([0, 1, 2, 3]),
dims=["sample", "group"],
coords={"group": ["Control", "Treatment A", "Treatment B", "Treatment C"]},
name="response",
)
fig = xpx(da_dist).box(title="Box Plot")
fig
Heatmap (imshow)¶
Best for 2D grid data:
In [11]:
Copied!
fig = xpx(da_2d).imshow(title="Heatmap")
fig
fig = xpx(da_2d).imshow(title="Heatmap")
fig
With different colorscale¶
In [12]:
Copied!
fig = xpx(da_2d).imshow(
color_continuous_scale="Viridis",
title="Heatmap with Viridis colorscale",
)
fig
fig = xpx(da_2d).imshow(
color_continuous_scale="Viridis",
title="Heatmap with Viridis colorscale",
)
fig
Faceting¶
All plot types support faceting to create subplot grids:
In [13]:
Copied!
# 3D data for faceting
da_3d = xr.DataArray(
np.random.randn(30, 3, 2).cumsum(axis=0),
dims=["time", "city", "scenario"],
coords={
"time": pd.date_range("2024-01-01", periods=30),
"city": ["NYC", "LA", "Chicago"],
"scenario": ["Low", "High"],
},
name="value",
)
fig = xpx(da_3d).line(
facet_col="city",
facet_row="scenario",
title="Faceted Line Plot",
)
fig
# 3D data for faceting
da_3d = xr.DataArray(
np.random.randn(30, 3, 2).cumsum(axis=0),
dims=["time", "city", "scenario"],
coords={
"time": pd.date_range("2024-01-01", periods=30),
"city": ["NYC", "LA", "Chicago"],
"scenario": ["Low", "High"],
},
name="value",
)
fig = xpx(da_3d).line(
facet_col="city",
facet_row="scenario",
title="Faceted Line Plot",
)
fig
Animation¶
Create animated plots by assigning a dimension to animation_frame:
In [14]:
Copied!
# Create monthly data
da_monthly = xr.DataArray(
np.random.rand(12, 4) * 100,
dims=["month", "product"],
coords={
"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
"product": ["A", "B", "C", "D"],
},
name="sales",
)
fig = xpx(da_monthly).bar(
x="product",
animation_frame="month",
title="Monthly Sales (Animated)",
range_y=[0, 120],
)
fig
# Create monthly data
da_monthly = xr.DataArray(
np.random.rand(12, 4) * 100,
dims=["month", "product"],
coords={
"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
"product": ["A", "B", "C", "D"],
},
name="sales",
)
fig = xpx(da_monthly).bar(
x="product",
animation_frame="month",
title="Monthly Sales (Animated)",
range_y=[0, 120],
)
fig